﻿Imports Negocio
Imports Utilitario
Public Class frmListarOrdenesCompra

    Dim servicio1 As New OrdenesCompraNegocio
    Dim util As New Utiles

    Dim dataOrdenes As New DataView

    Dim fecha As String = ""

    Sub listarOrdenesCompra(fecha As String)
        Cursor.Current = Cursors.WaitCursor
        Dim ds As DataSet = servicio1.obtenerOrdenesCompraXFecha(fecha, IIf(chkNacional.Checked = True, "1", "0"), IIf(chkImportado.Checked = True, "1", "0"))
        dataOrdenes = ds.Tables(0).DefaultView

        dgvOrdenes.DataSource = dataOrdenes

        If dataOrdenes.Count > 0 Then
            dgvOrdenes.Rows(0).Selected = False
        End If
        Cursor.Current = Cursors.Default
    End Sub

    Private Sub frmListarOrdenesCompra_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        fecha = CDate(txtFecha.Text).ToString("MM/dd/yyyy")
        listarOrdenesCompra(fecha)
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        listarOrdenesCompra(fecha)
    End Sub

    Private Sub dgvOrdenes_CellContentClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvOrdenes.CellContentClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            Dim frmOrden As New frmOrdenCompra(dgvOrdenes.Item("Column1", f).Value, dgvOrdenes.Item("Column2", f).Value,
                                               dgvOrdenes.Item("Column3", f).Value, dgvOrdenes.Item("Column4", f).Value, dgvOrdenes.Item("Column5", f).Value,
                                               dgvOrdenes.Item("Column7", f).Value)
            If frmOrden.ShowDialog = Windows.Forms.DialogResult.OK Then
                listarOrdenesCompra(fecha)
            End If
        End If
    End Sub

    Private Sub txtFecha_ValueChanged(sender As System.Object, e As System.EventArgs) Handles txtFecha.ValueChanged
        fecha = CDate(txtFecha.Text).ToString("MM/dd/yyyy")
    End Sub

    Private Sub chkNacional_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkNacional.CheckedChanged
        If chkNacional.Checked = False Then
            chkImportado.Checked = True
        End If
    End Sub

    Private Sub chkImportado_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkImportado.CheckedChanged
        If chkImportado.Checked = False Then
            chkNacional.Checked = True
        End If
    End Sub

    Private Sub dgvOrdenes_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvOrdenes.CellFormatting
        For i = 0 To dgvOrdenes.Rows.Count - 1
            If dgvOrdenes.Item("Column9", i).Value = "0" And e.RowIndex = i Then
                e.CellStyle.BackColor = Color.SkyBlue
            ElseIf dgvOrdenes.Item("Column9", i).Value = "1" And e.RowIndex = i Then
                e.CellStyle.BackColor = Color.PaleGreen
            End If
        Next
    End Sub

    Private Sub txtOrden_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtOrden.TextChanged
        dataOrdenes.RowFilter = "docto LIKE '%" & txtOrden.Text.Trim & "%'"
    End Sub

End Class